<?php
/**
 * Get the file requested and sends it
 * 
 * @author Sean
 * @return the file requested
 */
 
if(isset($_GET['googleId'], $_GET['sessionId'], $_GET['fileToDownload']))
{
 
	include ("connect.php");
	include ("config.php");

	$GoogleID = $_GET['googleId'];
	$GoogleID = mysql_real_escape_string($GoogleID);

	$SessionID = $_GET['sessionId'];
	$SessionID = mysql_real_escape_string($SessionID);

	$fileToDownload = $_GET['fileToDownload'];


	// Checks if the sessionid is valid
	$result = mysql_query("SELECT sessionID FROM users WHERE GoogleID = '" . $GoogleID . "'");
	while ($row = mysql_fetch_array($result)) {
	   $DB_SessionID = $row["sessionID"];
	}

	if($SessionID == $DB_SessionID){

		$fileToDownload = $pathToUsersFiles.$GoogleID."/".$fileToDownload;

		if (file_exists($fileToDownload))
		{
			// Sets the headers
			header('Content-Description: File Transfer');
			header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename='.basename($fileToDownload));
			header('Content-Transfer-Encoding: binary');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
			header('Content-Length: ' . filesize($fileToDownload));
			header('Access-Control-Allow-Origin: *');
			ob_clean();
			flush();

			readfile($fileToDownload);

			exit;
		}
		else{
			echo "ERROR: File ".$fileToDownload." does not exist!";
		}
	}
	else{
		header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=Login');
	}
}
?>